package guilayer;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import modellayer.Clerk;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import controllayer.OrderCtrl;
import controllayer.PersonCtrl;

public class MainLogin extends JPanel {
	private JTextField txtClerkid;
	private JPasswordField pwdClerkpwd;
	private JLabel lblLoginError;
	private OrderCtrl orderCtrl;
	private PersonCtrl personCtrl;

	/**
	 * Create the panel.
	 */
	public MainLogin() {
		orderCtrl = OrderCtrl.getInstance();
		personCtrl = PersonCtrl.getInstance();
		JPanel pnlLogin = new JPanel();
		pnlLogin.setBorder(new TitledBorder(null, "Medarbejder log ind!"));
		add(pnlLogin);
		
		pnlLogin.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		lblLoginError = new JLabel("Der er angivet forkert loginoplysnigner!");
		lblLoginError.setForeground(Color.RED);
		pnlLogin.add(lblLoginError, "2, 2, 3, 1");
		showErrorLabel(false);
		
		JLabel lblMedarbejderId = new JLabel("Medarbejder ID:");
		pnlLogin.add(lblMedarbejderId, "2, 4, right, default");
		
		txtClerkid = new JTextField();
		pnlLogin.add(txtClerkid, "4, 4, fill, default");
		txtClerkid.setColumns(10);
		
		JLabel lblAdgangskode = new JLabel("Adgangskode:");
		pnlLogin.add(lblAdgangskode, "2, 6, right, default");
		
		pwdClerkpwd = new JPasswordField();
		pwdClerkpwd.addKeyListener(new KeyListener() {
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyChar()==KeyEvent.VK_ENTER) {
					doLogin();
				}
			}
		});
		pnlLogin.add(pwdClerkpwd, "4, 6, fill, default");
		
		JButton btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				doLogin();
			}
		});
		pnlLogin.add(btnLogin, "2, 8, 3, 1");

	}
	
	private void callRootPane(int leftMenu, int mainContent, Object data) {
		((MainWindow)(this.getRootPane().getParent())).refreshAll(leftMenu, mainContent, data);
	}
	
	private void showErrorLabel(boolean show) {
		lblLoginError.setVisible(show);
	}

	private void doLogin() {
		Clerk clerk = personCtrl.getClerk(txtClerkid.getText());
		boolean success = true; // When password is added, change standard value to false!
		
		if (clerk!=null) {
			/*if (clerk.checkPassword(pwdClerkpwd.getPassword())) {
				success = true;
			}*/
			if (success) {
				orderCtrl.setClerk(clerk.getClerkNo());
				callRootPane(0, 0, null);
				updateUI();
			} else {
				showErrorLabel(true);
			}
		} else {
			showErrorLabel(true);
		}
	}
	
}
